local rule = fk.CreateSkill {
  name = "#wdtx_rule",
}

local WD = require "packages/wdtx/WdtxData"
local GensLimts = require "packages/wdtx/GensLimts"

rule:addEffect(fk.GameStart,{
  priority = 0.001,
  can_refresh = function (self, event, target, player, data)
    return player.id > 0 and player.role == "lord"
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local Wdtx = room.logic.Wdtx
    room:setTag("startwdtx",0)
    local humans = 0
    for _, pr in ipairs(room.players) do
      if pr.id < 0 then
        if pr.role == "rebel" then
          room:killPlayer({ who = pr })
        end
      else
        humans = humans + 1
      end
    end
    if humans ~= 1 then
      return room:gameOver("rebel")
    end
    room:setTag("startwdtx",1)
    Wdtx:addInitCardsToArea(table.clone(WD:getInitCards()))
    room:handleAddLoseSkills(player, "#wdtx_modskill", nil, false)
    -- if IStest then
    --   room:handleAddLoseSkills(player, "|damage_maker|cheat|rende|wdtx_xianyu", nil, false)--|damage_maker|cheat|rende
    -- end
    -- player:throwAllCards("h")
    for _, trig in ipairs(Fk.global_trigger) do
      if trig:getSkeleton().name == "mobile_effect" then  --开手杀特效
        room.logic:addTriggerSkill(trig)
      end
    end
    Wdtx:nextStory()
  end,
})

-- rule:addEffect(fk.BuryVictim, {
--   priority = 0.001,
--   can_refresh = function (self, event, target, player, data)
--     return player == target
--   end,
--   on_refresh = function (self, event, target, player, data)
--     local room = player.room
--     room:setTag("SkipGameRule", true)
--     room:setTag("SkipNormalDeathProcess", true)
--   end,
-- })
rule:addEffect(fk.BuryVictim, {
  priority = 0.001,
  can_refresh = function (self, event, target, player, data)
    return player.id > 0 and player.role == "lord"
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setTag("SkipGameRule", true)
    local e = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
    if not target.dead or not e then return end
    -- if room:getTag("startwdtx") == 0 then return end
    local Wdtx = room.logic.Wdtx
    if data.who == player then
      room:gameOver("rebel")
      return
    end
    local tar = player.rest <= 0 and player or room:getPlayerById(room:getTag("mainid"))
    -- local tar = room:getLord()
    local onlymy = table.every(room.alive_players,function(pr) return GensLimts:isSameCamp(pr,tar) end)
    if not onlymy then return end
    -- local rebelalldead = table.every(room.alive_players,function(pr) return GensLimts:isSameCamp(pr,tar) end)
    -- if not rebelalldead then return end
    Wdtx:defeatAllRobots()
  end,
})

rule:addEffect(fk.RoundEnd, {
  priority = 0.001,
  can_refresh = function (self, event, target, player, data)
    return player.id > 0 and player.role == "lord"
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local Wdtx = room.logic.Wdtx
    local alive = table.filter(room.players, function(p) return not (p.dead and p.rest == 0) end)
    if #alive == 1 then
      if alive[1].id > 0 then
        Wdtx:defeatAllRobots()
      end
    end
  end,
})

rule:addEffect(fk.GameOverJudge, {
  priority = 0.001,
  can_refresh = function (self, event, target, player, data)
    return player.id > 0 and player.role == "lord"
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setTag("SkipGameRule", true)
    if room:getTag("startwdtx") == 0 then return end
    local Wdtx = room.logic.Wdtx
    local status = Wdtx:getStoryStatus()
    if data.who.id < 0 then
      local tar = data.who
      if not Wdtx:curPlayerisTriEvent(tar.general) then
        Wdtx:changeSoldier(tar)
      end
    else
      -- if status ~= Story.StatusDeath then
      --   Wdtx:handleStoryStatus()
      -- end
    end
  end,
})

rule:addEffect(fk.PreCardEffect, {
  priority = 0.001,
  can_refresh = function (self, event, target, player, data)
    if #player:getAvailableEquipSlots(Card.SubtypeArmor) == 0 or player:getMark(MarkEnum.MarkArmorNullified) > 0 then return false end
    local id = player:getEquipment(Card.SubtypeArmor)
    if data.to == player and id and Fk:getCardById(id).trueName == "vine" then
      return table.contains({"wdtx__slash", "wdtx__savage_assault", "wdtx__archery_attack"}, data.card.name)
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:broadcastPlaySound("./packages/maneuvering/audio/card/vine")
    room:setEmotion(player, "./packages/maneuvering/image/anim/vine")
    data.nullified = true
  end,
})


return rule
